﻿Namespace mnsrulz.mSongsModels
#Region "Models"
    Public Class SongModel
        Property ID As Integer
        Property Title As String
        Property DownloadURL As String
        Property AlbumID As Integer
        Property Album As AlbumModel

        Shared Function Transform(ByVal s As String) As SongModel
            Dim objSong As New SongModel()
            objSong.Album = New AlbumModel

            s = System.Text.RegularExpressions.Regex.Replace(s, "\t|\n|\r", "")
            Dim rxDwldURL = System.Text.RegularExpressions.Regex.Match(s, "http://download[^""]*")

            If rxDwldURL.Success Then
                objSong.DownloadURL = rxDwldURL.Value
                objSong.ParseTitle()
            Else
                Return Nothing
            End If

            Dim rxDirPath = System.Text.RegularExpressions.Regex.Match(s, "<div id=""dvdirpath"">.*?</div>")

            If rxDirPath.Success Then
                Dim dirPathComplete = rxDirPath.Value
                Dim rxDirLinks = System.Text.RegularExpressions.Regex.Matches(dirPathComplete, "<a[^>]*?>.*?</a>")
                For Each rxMatch As System.Text.RegularExpressions.Match In rxDirLinks
                    Dim dirPathValue = rxMatch.Value
                    dirPathValue = System.Text.RegularExpressions.Regex.Replace(dirPathValue, "<[^>]*>", "")

                    If String.IsNullOrEmpty(objSong.Album.Title) Then
                        objSong.Album.Title = dirPathValue
                    Else
                        objSong.Album.Title &= " / " & dirPathValue
                    End If
                Next
            End If

            Return objSong

        End Function

        Private Sub ParseTitle()
            Dim objSplitItems = Me.DownloadURL.Split("/").ToList
            Me.Title = objSplitItems.Item(objSplitItems.Count - 1)
        End Sub
    End Class

    Public Class AlbumModel
        Property ID As Integer
        Property Title As String
        Property Year As String
        Property Songs As SongsModel
    End Class

    Public Class SongsModel
        Inherits List(Of SongModel)
        Property TotalCount As Integer
        Property StartRow As Integer
        Property ItemCount As Integer
    End Class

    Public Class AlbumsModel
        Inherits List(Of AlbumModel)
        Property TotalCount As Integer
        Property StartRow As Integer
        Property ItemCount As Integer
    End Class
#End Region

#Region "Services"
    Public Class SongService

        Function ListSongs(ByVal search As String, ByVal startIndex As Integer, _
                           ByVal maxRows As Integer) As SongsModel
            Dim lstModel As New SongsModel
            For i = 0 To 9
                Dim o As New SongModel With {.AlbumID = 12, .ID = 12, .Title = "title" & i}
                lstModel.Add(o)
            Next
            lstModel.StartRow = 1
            lstModel.TotalCount = 10
            lstModel.ItemCount = 10
            Return lstModel
        End Function

        Shared Sub InsertSong(ByVal objSong As SongModel)
            Dim db As New mSongs.BO.mSongsDBDataContext
            db.usp_insert_fmwsong(objSong.ID, objSong.Title, objSong.DownloadURL, objSong.Album.Title)
        End Sub

        Shared Function GetLastFMWSongInserted() As Integer
            Dim db As New mSongs.BO.mSongsDBDataContext
            Dim lstInsertedId = (From t In db.Songs Order By t.Id Descending Select t.Id).FirstOrDefault
            Return lstInsertedId
        End Function
    End Class

    Public Class AlbumService

        Shared Function ListAlbums() As AlbumsModel
            Dim lstModel As New AlbumsModel
            For i = 0 To 99
                Dim o As New AlbumModel With {.ID = i, .Title = "Album" & i, .Year = 1998}
                o.Songs = New SongsModel
                For j = 0 To 5
                    Dim k As New SongModel With {.AlbumID = i, .ID = i * j, .Title = "title" & i * j}
                    o.Songs.Add(k)
                Next
                lstModel.Add(o)
            Next
            Return lstModel
        End Function

        Shared Function GetAlbumByID(ByVal id As Integer) As AlbumModel
            Return (From t In ListAlbums() Where t.ID = id Select t).FirstOrDefault
        End Function

        Sub InsertAlbum(ByVal objAlbum As AlbumModel)
            Dim db As New mSongs.BO.mSongsDBDataContext
            db.usp_insert_album(objAlbum.Title, objAlbum.Year)
        End Sub

    End Class
#End Region

End Namespace